<template>
  <div class="box">
    <!-- 购物车列表 -->
    <el-form class="form">
      <h2 class="title">我的购物车</h2>
      <el-table width="100%" :data="fData">
        <!-- 全选按钮 -->
        <!-- <el-table-column
          change="changeCheckout"
          type="selection"
          width="30px"
        ></el-table-column> -->
        <!-- <el-table-column label="全选" width="70px"></el-table-column> -->
        <el-table-column label="选择" width="100px">
          <template slot-scope="scope">
            <el-checkbox
              :checked="scope.row.isChecked"
              @change="changeCheck(scope.row.id, $event)"
            ></el-checkbox>
          </template>
        </el-table-column>
        <el-table-column label="封面" width="80px">
          <template slot-scope="scope">
            <!-- 商品图片 -->
            <img :src="scope.row.cover" width="50px" alt="" />
          </template>
        </el-table-column>
        <el-table-column label="商品信息" width="200px">
          <template slot-scope="scope">
            <div class="product">
              {{ scope.row.title }}
              <p>{{ scope.row.modul }}</p>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="单价(元)">
          <template slot-scope="scope">
            <span>￥{{ scope.row.price }}</span>
          </template>
        </el-table-column>
        <el-table-column label="数量" width="150px">
          <template slot-scope="scope">
            <el-input-number
              size="small"
              v-model="scope.row.saleCount"
              @change="handleChange(scope.row, $event)"
              :min="1"
              :max="10"
              label="描述文字"
            ></el-input-number>
          </template>
        </el-table-column>
        <el-table-column label="小计(元)">
          <template slot-scope="scope">
            <span>￥{{ getSum(scope.row.price, scope.row.saleCount) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <!-- <el-button type="text" size="small">移入收藏夹</el-button> -->
            <el-button
              type="text"
              size="small"
              @click="deleted(scope.row.id, scope.row.index)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination layout="->,prev, pager, next" :total="50"> </el-pagination>
      <div class="settlement">
        <div>
          <span>合计: {{ allPrice }} 元</span>
          <el-button
            type="primary"
            @click="handlerPay"
            style="background: black; border-color: black"
            >结算</el-button
          >
        </div>
      </div>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      num: 1,
      fData: [],
      count: "",
      total1: 0,
      uid: "",
    };
  },
  mounted() {
    this.getUser();
  },
  computed: {
    allPrice() {
      console.log(this.fData, "FdATA");
      let r = 0;
      let filterList = this.fData.filter((item) => {
        return item.isChecked;
      });
      filterList.forEach((item) => {
        // console.log(item);
        let p =
          item.price.indexOf("￥") > -1
            ? item.price.split("￥")[1]
            : item.price;
        r += p * item.saleCount;
      });
      return r;
    },
  },
  methods: {
    changeCheck(id, status) {
      this.fData.find((item) => {
        if (item.id == id) {
          item.isChecked = status;
          return true;
        }
      });
    },
    // // 计算小计的方法
    getSum(price, saleCount) {
      return price * (this.count != 0 ? this.count : saleCount);
    },
    // 计数器方法
    handleChange(obj, num) {
      this.fData.forEach((item) => {
        if (obj.id == item.id) {
          item.saleCount = num;
        }
      });
    },
    // 点击删除的方法
    deleted(id, index) {
      console.log(index);
      this.$http.orderApi.delete({ id }).then((res) => {
        // console.log(res);
        if (res.data.code == 200) {
          alert("删除成功");
          // this.fData = this.fData;
          this.getUser();
        }
      });
    },
    // 请求用户的数据
    getUser() {
      let user = sessionStorage.getItem("user");
      user = JSON.parse(user);
      let { uid } = user;
      this.uid = uid;
      this.$http.orderApi.queryByUid({ uid }).then((res) => {
        console.log(res, "111");
        res.data.res.forEach((item) => {
          item.isChecked = false;
        });
        let data = res.data.res;
        data.forEach((item) => {
          // 判断字符串转数字是否NAN
          if (isNaN(item.price)) {
            console.log("不是数字 " + item.price);
            item.price = item.price.substring(1);
          }

          // 判断是否已支付
          if (item.payed == 0) {
            this.fData.push(item);
          }
        });
      });
    },

    sun() {
      var total = 0;
      this.chenckList.forEach((item) => {
        total += item.price * item.number;
      });
      console.log(total);
      return (this.total1 = total);
    },
    // 点击结算传递参数
    handlerPay() {
      let list = [];
      let pay = this.fData.filter((item) => {
        if (item.isChecked) {
          list.push({ id: item.id, saleCount: item.saleCount });
        }
      });
      this.$router.push({
        path: "/buy",
        query: {
          uid: this.uid,
          list: JSON.stringify(list),
        },
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.title {
  margin: 20px 0;
}
.box {
  width: 90%;
  margin: 0 auto;
  ::deep .form {
    width: 100%;
  }
}
h4 {
  margin-bottom: 10px;
}
// .subbotals {
//   color: #e6a23c;
// }
.settlement {
  position: relative;
  width: 100%;
  > div {
    position: absolute;
    top: 5px;
    right: 15px;
    > span {
      margin-right: 10px;
    }
  }
}
</style>